package com.anis.medical.saving;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.anis.jpa.medical.model.MedicalReport;
import com.anis.jpa.medical.model.Patient;
import com.anis.jpa.medical.model.Templates;

public class SaveRetrieveData {

	private EntityManagerFactory emf;
	private EntityManager em;
	private Query patientQuery = null;
	private List<Patient> result = null;

	public SaveRetrieveData() {
		super();
		emf = Persistence.createEntityManagerFactory("com.anis.medical.jpa");
		em = emf.createEntityManager();
	}

	public String savePatient(Patient patient) {
		em = emf.createEntityManager();
		em.getTransaction().begin();
		String id = patient.getIdpatient();
		if (id == null) {
			em.persist(patient);
			id = patient.getIdpatient();
		} else
			em.merge(patient);
		em.flush();
		em.getTransaction().commit();
		em.close();
		return id;
	}

	public void savePatientReport(Patient patient, MedicalReport report) {
		em = emf.createEntityManager();
		em.getTransaction().begin();
		patient.addRadioreport(report);
		em.merge(patient);
		em.merge(report);
		em.flush();
		em.getTransaction().commit();
		em.close();
	}

	public List<Patient> retrievePatient(String patientname,
			String patientLastName, Integer patientPhone, Integer patientID) {
		Query patientQuery = null;
		List<Patient> result = null;
		em = emf.createEntityManager();
		em.getTransaction().begin();
		if (patientname != null && patientLastName == null
				&& patientPhone == null && patientID == null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientname=:patientname");
			patientQuery.setParameter("patientname", patientname);
		} else if (patientname != null && patientLastName != null
				&& patientPhone == null && patientID == null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientname=:patientname AND p.patientlastname=:patientlastname");
			patientQuery.setParameter("patientname", patientname);
			patientQuery.setParameter("patientlastname", patientLastName);

		} else if (patientname != null && patientLastName != null
				&& patientPhone != null && patientID == null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientname=:patientname AND p.patientlastname=:patientlastname AND p.phone=:phone");
			patientQuery.setParameter("patientname", patientname);
			patientQuery.setParameter("patientlastname", patientLastName);
			patientQuery.setParameter("phone", patientPhone);

		} else if (patientname != null && patientLastName != null
				&& patientPhone != null && patientID != null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientname=:patientname AND p.patientlastname=:patientlastname AND p.phone=:phone AND p.idpatient=:idpatient");
			patientQuery.setParameter("patientname", patientname);
			patientQuery.setParameter("patientlastname", patientLastName);
			patientQuery.setParameter("phone", patientPhone);
			patientQuery.setParameter("idpatient", patientID);
		} else if (patientname == null && patientLastName != null
				&& patientPhone == null && patientID == null) {

			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientlastname=:patientlastname");
			patientQuery.setParameter("patientlastname", patientLastName);

		} else if (patientname == null && patientLastName != null
				&& patientPhone != null && patientID == null) {

			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientlastname=:patientlastname AND p.phone=:phone");
			patientQuery.setParameter("patientlastname", patientLastName);
			patientQuery.setParameter("phone", patientPhone);

		} else if (patientname == null && patientLastName != null
				&& patientPhone != null && patientID != null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientlastname=:patientlastname AND p.phone=:phone AND p.idpatient=:idpatient");
			patientQuery.setParameter("patientlastname", patientLastName);
			patientQuery.setParameter("phone", patientPhone);
			patientQuery.setParameter("idpatient", patientID);

		} else if (patientname == null && patientLastName == null
				&& patientPhone != null && patientID == null) {

			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.phone=:phone");
			patientQuery.setParameter("phone", patientPhone);

		} else if (patientname == null && patientLastName == null
				&& patientPhone != null && patientID != null) {

			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.phone=:phone AND p.idpatient=:idpatient");
			patientQuery.setParameter("phone", patientPhone);
			patientQuery.setParameter("idpatient", patientID);

		} else if (patientname == null && patientLastName == null
				&& patientPhone == null && patientID != null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.idpatient=:idpatient");
			patientQuery.setParameter("idpatient", String.valueOf(patientID));

		} else if (patientname != null && patientLastName == null
				&& patientPhone == null && patientID != null) {
			patientQuery = em
					.createQuery("select p FROM Patient p WHERE p.patientname=:patientname AND p.idpatient=:idpatient");
			patientQuery.setParameter("patientname", patientname);
			patientQuery.setParameter("idpatient", patientID);
		} else {
			System.out.print("Case not Taken in consideration please note it");
		}
		if (patientQuery != null) {
			result = patientQuery.getResultList();
			em.getTransaction().commit();
			em.close();
		} else {
			result = null;
		}

		return result;

	}

	public List<Patient> retrieveAllPatient() {
		em = emf.createEntityManager();
		em.getTransaction().begin();
		patientQuery = em.createQuery("select p FROM Patient p");
		result = patientQuery.getResultList();
		em.getTransaction().commit();
		em.close();
		return result;
	}

	public List<Patient> retrievePatientWithRDVOnDate(Date rdvdate) {
		em = emf.createEntityManager();
		em.getTransaction().begin();
		patientQuery = em
				.createQuery("select p FROM Patient p WHERE p.rdvdate=:rdvdate");
		patientQuery.setParameter("rdvdate", rdvdate);
		result = patientQuery.getResultList();
		em.getTransaction().commit();
		em.close();
		return result;
	}

	public void saveTemplate(Templates template) {
		em = emf.createEntityManager();
		em.getTransaction().begin();
		em.merge(template);
		em.flush();
		em.persist(template);
		em.getTransaction().commit();
		em.close();
	}

	public List<Templates> retrieveTemplate(String tempname) {
		em = emf.createEntityManager();
		Query templatesQuery = null;
		em.getTransaction().begin();
		templatesQuery = em
				.createQuery("select p FROM Templates p WHERE p.tempname=:tempname");
		templatesQuery.setParameter("tempname", tempname);
		List<Templates> resultTemplates = templatesQuery.getResultList();
		em.getTransaction().commit();
		em.close();
		return resultTemplates;
	}
}
